/**
 * 
 */
package com.sip.comadapter.executor;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.sip.comadapter.common.BaseExecutor;
import com.sip.comadapter.executor.common.Executor;
import com.sip.comserver.communication.CommunicationMessage;
import com.sip.dbadapter.dao.domain.ApiDefinition;
import com.sip.dbadapter.dao.domain.ReleaseVersion;
import com.sip.dbadapter.dao.domain.ScenarioRecord;
import com.sip.dbadapter.dao.domain.SystemInfo;
import com.sip.dbadapter.dao.interfaces.ApiDao;
import com.sip.dbadapter.dao.interfaces.RecordAndPlaySearchDataDao;
import com.sip.dbadapter.dao.utility.DaoLoadFactory;
import com.sip.dbadapter.dao.utility.DaoNameConstant;
import com.sip.dbadapter.domain.registry.DomainRegistry;

/**
 * @author mg40327
 *
 */
public class RecordAndPlaySearchExecutor extends BaseExecutor implements Executor {

	@Override
	public CommunicationMessage execute(CommunicationMessage message)
			throws Exception {
		// TODO Auto-generated method stub
		
		System.out.println(" ==================== RecordAndPlaySearchExecutor ========================");
		
		final Map<String, Object> domainMap = (Map<String, Object>) message.getObject(); 
		
		final ApiDefinition apiDefinitionDomainObj = (ApiDefinition) domainMap.get(DomainRegistry.API_DEFINITION);
		final ScenarioRecord scenarioRecordDomainObj =(ScenarioRecord) domainMap.get(DomainRegistry.SCENARIO_RECORD);
		final ReleaseVersion releaseVersionDomainObj= (ReleaseVersion)domainMap.get(DomainRegistry.RELEASE_VERSION);
		final SystemInfo systemInfoDomainObj=(SystemInfo)domainMap.get(DomainRegistry.SYSTEM_INFO);
		final Map dateMap=(Map)domainMap.get(DomainRegistry.DOMAIN_RELATED_GENERIC_INFO);
		
		
		System.out.println("*******************************"+apiDefinitionDomainObj.getName());
		String apiName=apiDefinitionDomainObj.getName();
		String startDate=null;
		String endDate=null;
		if(dateMap.size()>1){
			startDate=(String)dateMap.get("startDate");
		    endDate=(String)dateMap.get("endDate");
		}
		System.out.println(""+dateMap.get("startDate")+" end date "+dateMap.get("endDate"));
		long apiId=0l;
		ApiDao apiDaoObj=loadApiDefinitionDao();
		List<ScenarioRecord> searchResultList=null;
		if(!("".equals(apiName)) || apiName!=null){
			
			apiId=apiDaoObj.getApiId(apiDefinitionDomainObj.getName());
		}
		
		RecordAndPlaySearchDataDao recordAndPlaysearchDatDaoObj=loadRecordAndPlaySearchDataDao();
        
		if(startDate!=null && endDate!=null && apiId>0){
			searchResultList=recordAndPlaysearchDatDaoObj.getSearchResult(apiId, startDate, endDate);
			
		}
		if(!("".equals(startDate) ) && !("".equals(endDate) ) && apiId>0){
			searchResultList=recordAndPlaysearchDatDaoObj.getSearchResult( apiId);
		}
		if(!("".equals(startDate) ) && !("".equals(endDate) ) &&  apiId<=0 && startDate!=null && endDate!=null){
			searchResultList=recordAndPlaysearchDatDaoObj.getSearchResult(startDate, endDate);
		}
		
		if(startDate==null && endDate==null &&  apiId<=0){
			searchResultList=recordAndPlaysearchDatDaoObj.getSearchResult();
		}
		
		List<ScenarioRecord> searchResultList1=new ArrayList<ScenarioRecord>();
		for(ScenarioRecord scenarioRecord:searchResultList){
			ScenarioRecord sr=scenarioRecord;
			ApiDefinition apiDef=scenarioRecord.getApiDefinitionScenarioRecord();
			final ApiDefinition apiDefinition=apiDaoObj.findById(apiDef.getApiId());
			sr.setApiDefinitionScenarioRecord(apiDefinition);
			searchResultList1.add(sr);
			
		}
		domainMap.put(DomainRegistry.SCENARIO_RECORD_LIST, searchResultList1);

		message.setObject(domainMap);
		
		return message;
	}
	
	  private ApiDao loadApiDefinitionDao() throws Exception {

		  ApiDao apiDef=(ApiDao)DaoLoadFactory.getDao(DaoNameConstant.API_DAO);
		  return apiDef;
		  
		  
	   
	  }
	  private RecordAndPlaySearchDataDao loadRecordAndPlaySearchDataDao() throws Exception {

		  RecordAndPlaySearchDataDao recordAndPlaySearchDao=(RecordAndPlaySearchDataDao)DaoLoadFactory.getDao(DaoNameConstant.RECORD_PLAY_SEARCH_DAO);
		  return recordAndPlaySearchDao;
		  
		  
	   
	  }

}